package com.zwc.angle2048;

import com.zwc.angle.AngleRotatingSprite;
import com.zwc.angle.AngleSprite;
import com.zwc.angle.AngleSpriteLayout;
import com.zwc.angle.AngleSurfaceView;

/**
 * 游戏卡片类
 * 
 * @author zwc
 *
 */
public class Card {
	private int width = GameUtil.WIDTH;
	private int height = GameUtil.HEIGHT;
	int cardX, cardY;

	/**
	 * 添加随机卡片 有透明效果
	 * 
	 * @param view
	 * @param x
	 * @param y
	 * @param num
	 * @return
	 */
	public AngleRotatingSprite addRandomCard(AngleSurfaceView view, int x,
			int y, int num) {

		cardX = width / 2 - width * 3 / 8 + y * width / 4;
		cardY = height / 2 - width * 3 / 8 + x * width / 4;
		AngleRotatingSprite randomCard = new ShowCard(cardX, cardY,
				new AngleSpriteLayout(view, getsId(num)));

		return randomCard;
	}

	/**
	 * 添加普通卡片
	 * 
	 * @param view
	 * @param x
	 * @param y
	 * @param num
	 * @return
	 */
	public AngleSprite addCard(AngleSurfaceView view, int x, int y, int num) {
		cardX = width / 2 - width * 3 / 8 + y * width / 4;
		cardY = height / 2 - width * 3 / 8 + x * width / 4;
		AngleSprite card = new AngleSprite(cardX, cardY, new AngleSpriteLayout(
				view, getsId(num)));
		return card;
	}

	public int getsId(int num) {
		int sId;
		switch (num) {
		case 0:
			sId = R.drawable.card0;
			break;
		case 2:
			sId = R.drawable.card2;
			break;
		case 4:
			sId = R.drawable.card4;
			break;
		case 8:
			sId = R.drawable.card8;
			break;
		case 16:
			sId = R.drawable.card16;
			break;
		case 32:
			sId = R.drawable.card32;
			break;
		case 64:
			sId = R.drawable.card64;
			break;
		case 128:
			sId = R.drawable.card128;
			break;
		case 256:
			sId = R.drawable.card256;
			break;
		case 512:
			sId = R.drawable.card512;
			break;
		case 1024:
			sId = R.drawable.card1024;
			break;
		case 2048:
			sId = R.drawable.card2048;
			break;
		case 4096:
			sId = R.drawable.card4096;
			break;
		case 8192:
			sId = R.drawable.card8192;
			break;
		case 16384:
			sId = R.drawable.card16384;
			break;
		case 32768:
			sId = R.drawable.card32768;
			break;
		case 65536:
			sId = R.drawable.card65536;
			break;
		default:
			sId = R.drawable.card0;
			break;
		}
		return sId;
	}

	class ShowCard extends AngleRotatingSprite {
		private static final float sAlphaSpeed = 1f;
		private float mAplhaDir;

		public ShowCard(int x, int y, AngleSpriteLayout layout) {
			super(x, y, layout);
			mAplhaDir = sAlphaSpeed;
		}

		@Override
		public void step(float secondsElapsed) {
			if (mAlpha < 1) {
				mAlpha += secondsElapsed * mAplhaDir;
			}else{
				mAlpha=1;
			}
			super.step(secondsElapsed);
		}

	}
}
